package com.qf.springmvc.controller;

import com.qf.springmvc.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @authorDXT
 * @date2022/4/29 17:30
 */
@Controller
@RequestMapping("data")
@SessionAttributes({"name","age"})
// model中的 name和age 会存入session中
public class DataController {
    // 原始的传值方式
    @RequestMapping("test1")
    public String test1(HttpServletRequest request, HttpSession session) {
        // 到request域中存数据
        request.setAttribute("name", "admin");
        request.setAttribute("age", 20);
        // session存数据
        session.setAttribute("date", "2022");
        return "welcome";
    }

    // Spring推荐的传值方式
    @RequestMapping("test2")
    public String test2(Model model) {
        // model中的数据是放到request中的
        model.addAttribute("name", "张三");
        model.addAttribute("age", 20);
        // 如果没有指定key的话，key默认就是value值的类型
        model.addAttribute("李四");
        User user = new User();
        user.setName("王五");
        user.setId(11);
        model.addAttribute(user);
        return "welcome";
    }

    @RequestMapping("test3")
    public ModelAndView test3() {
        ModelAndView modelAndView = new ModelAndView("welcome");
        modelAndView.addObject("name","赵六");
        return modelAndView;
    }
}
